<?php

namespace App;

use function Composer\Autoload\includeFile;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class AboutModel extends Model
{
    // 列出关于我们成员
    public static function about_us_list()
    {
        $list = DB::table('about_us')->select('id', 'name', 'job', 'avatar')->get();
        return $list;
    }

    // 列出关于我们各种文字
    public static function about_us_msg()
    {
        $lists = DB::table('about_other')->select('id', 'name', 'text')->orderBy('id', 'asc')->get();
        $company_recommend = '';
        $tel = '';
        $fax = '';
        $add = '';
        $wx = '';
        $name = '';
        $icp = '';
        $copyright = '';
        $rttel = '';
        $logo = '';
        foreach ($lists as $list) {
            $text = $list->text;
            switch ($list->name) {
                case '公司介绍':
                    $company_recommend = $text;
                    break;
                case '公司电话':
                    $tel = $text;
                    break;
                case '公司传真':
                    $fax = $text;
                    break;
                case '公司地址':
                    $add = $text;
                    break;
                case '微信二维码':
                    $wx = $text;
                    break;
                case '公司名称':
                    $name = $text;
                    break;
                case '备案':
                    $icp = $text;
                    break;
                case '版权':
                    $copyright = $text;
                    break;
                case '右上角电话':
                    $rttel = $text;
                    break;
                case '公司LOGO':
                    $logo = $text;
                    break;
                default:
                    break;
            }
        }
        $res_array = [
            '公司介绍' => $company_recommend,
            '公司电话' => $tel,
            '公司传真' => $fax,
            '公司地址' => $add,
            '微信二维码' => $wx,
            '公司名称' => $name,
            '备案' => $icp,
            '版权' => $copyright,
            '右上角电话' => $rttel,
            '公司LOGO' => $logo
        ];
        return $res_array;
    }

    // 列出关于我们所有图片
    public static function about_us_image()
    {
        $lists = DB::table('about_image')->select('id', 'file')->orderBy('id', 'asc')->get();
        $res_array = [];
        foreach ($lists as $list) {
            array_push($res_array, $list->file);
        }
        return $res_array;
    }

    public static function update_about_other($update_array)
    {
        foreach ($update_array as $key => $value) {
            DB::table('about_other')->where('name', $key)->update(['text' => $value]);
        }
        return 0;
    }

    public static function update_about_us($id, $update_array)
    {
        if (DB::table('about_us')->where('id', $id)->exists()) {
            DB::table('about_us')->where('id', $id)->update([
                'name' => $update_array['name'],
                'job' => $update_array['job'],
                'avatar' => $update_array['avatar']
            ]);
            return 0;
        } else {
            return 1;
        }
    }

    public static function add_about_us($id, $add_array)
    {
        DB::table('about_us')->insert([
            'id' => $id,
            'name' => $add_array['name'],
            'job' => $add_array['job'],
            'avatar' => $add_array['avatar']
        ]);
        return 0;
    }

    public static function delete_about_us($id)
    {
        if (DB::table('about_us')->where('id', $id)->exists()){
            DB::table('about_us')->where('id', $id)->delete();
            return 0;
        } else {
            return 1;
        }
    }
}
